/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PedidoPrevisaoDialogForm.java
 *
 * Created on 19/11/2013, 21:35:29
 */
package Formularios;

import Classes.ClientesClass;
import Classes.ProdutoClass;
import Classes.ProdutoPSemanaClass;
import DAO.ClienteDAO;
import DAO.ProdutoDAO;
import DAO.ProdutoPSemanaDAO;
import java.awt.Color;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;

/**
 *
 * @author Carlos Schaefer
 */
public class PedidoPrevisaoDialogForm extends javax.swing.JDialog {

    
    
    
    
    
    
    /** Creates new form PedidoPrevisaoDialogForm */
    public PedidoPrevisaoDialogForm(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(null);
        getContentPane().setBackground(Color.WHITE);
    }
    
    private int modo; //1 = Inserindo

    
    public void setProdutoPsemana(ProdutoPSemanaClass produtoPsemana) {
        this.produtoPsemana = produtoPsemana;
    }
    
    public void setModo(int modo) {
        this.modo = modo;
    }
    
    
    
    
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 0));
        jLabel1 = new javax.swing.JLabel();
        txtAno = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtSemana = new javax.swing.JTextField();
        cboxProduto = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtValor = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtDataConclusao = new javax.swing.JTextField();
        cboxCliente = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        filler2 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 0));
        jButton2 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        chboxClie = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel1.setText("Manutenção de Previsão/Pedidos");

        jLabel2.setText("Ano");

        jLabel3.setText("Semana");

        cboxProduto.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel4.setText("Produto");

        jLabel5.setText("Valor");

        jLabel6.setText("Data de Conclusão");

        cboxCliente.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cboxCliente.setEnabled(false);

        jLabel8.setText("Cliente");

        jButton2.setText("Salvar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton1.setText("Cancelar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        chboxClie.setText("Selcionar Cliente");
        chboxClie.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                chboxClieMouseClicked(evt);
            }
        });
        chboxClie.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chboxClieActionPerformed(evt);
            }
        });
        chboxClie.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                chboxCliePropertyChange(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(filler2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addContainerGap(72, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(txtAno, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(txtSemana, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jLabel6)
                    .addComponent(jLabel4)
                    .addComponent(txtDataConclusao, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cboxProduto, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(txtValor, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cboxCliente, 0, 292, Short.MAX_VALUE)
                    .addComponent(jLabel8)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(chboxClie))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtAno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtSemana, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cboxProduto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtValor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtDataConclusao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(14, 14, 14)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(filler2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chboxClie)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cboxCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(55, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
    
    MontarComboProduto();
    
    MontarComboCliente();
    PreencherCampos();
    
    
}//GEN-LAST:event_formWindowOpened

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
  if (modo == 1){
        produtoPsemana = new ProdutoPSemanaClass();
        produtoPsemana.setAno(Integer.parseInt(txtAno.getText()));
        produtoPsemana.setNumerosemana(Integer.parseInt(txtSemana.getText()));
        if (chboxClie.isSelected()){
        produtoPsemana.setCliente((ClientesClass) cboxCliente.getModel().getSelectedItem());
        }
        produtoPsemana.setProduto((ProdutoClass) cboxProduto.getModel().getSelectedItem());
        produtoPsemana.setValor(Double.parseDouble(txtValor.getText()));
        produtoPsemana.setDataconclusao (new Date(txtDataConclusao.getText()));
        dao.insert(produtoPsemana);
   }else{
      
        produtoPsemana.setAno(Integer.parseInt(txtAno.getText()));
        produtoPsemana.setNumerosemana(Integer.parseInt(txtSemana.getText()));
        if (chboxClie.isSelected()){
        produtoPsemana.setCliente((ClientesClass) cboxCliente.getModel().getSelectedItem());
        }
        produtoPsemana.setProduto((ProdutoClass) cboxProduto.getModel().getSelectedItem());
        produtoPsemana.setValor(Double.parseDouble(txtValor.getText()));
        produtoPsemana.setDataconclusao (new Date(txtDataConclusao.getText()));
        dao.insert(produtoPsemana);
        produtoPsemana.setDataconclusao (new Date(txtDataConclusao.getText()));
        dao.update(produtoPsemana);
   }    
    this.dispose();
   
}//GEN-LAST:event_jButton2ActionPerformed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.setVisible(false);
}//GEN-LAST:event_jButton1ActionPerformed

private void chboxClieActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chboxClieActionPerformed
 
  
  
  
  
}//GEN-LAST:event_chboxClieActionPerformed

private void chboxClieMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_chboxClieMouseClicked

}//GEN-LAST:event_chboxClieMouseClicked

private void chboxCliePropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_chboxCliePropertyChange
  if (chboxClie.getVerifyInputWhenFocusTarget()){
   cboxCliente.enable(true);   
      
  }else{
      cboxCliente.enable(false);
  }
}//GEN-LAST:event_chboxCliePropertyChange

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PedidoPrevisaoDialogForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PedidoPrevisaoDialogForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PedidoPrevisaoDialogForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PedidoPrevisaoDialogForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                PedidoPrevisaoDialogForm dialog = new PedidoPrevisaoDialogForm(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cboxCliente;
    private javax.swing.JComboBox cboxProduto;
    private javax.swing.JCheckBox chboxClie;
    private javax.swing.Box.Filler filler1;
    private javax.swing.Box.Filler filler2;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField txtAno;
    private javax.swing.JTextField txtDataConclusao;
    private javax.swing.JTextField txtSemana;
    private javax.swing.JTextField txtValor;
    // End of variables declaration//GEN-END:variables
    ProdutoDAO pdao = new ProdutoDAO();
    ClienteDAO clidao = new ClienteDAO();
    ProdutoPSemanaDAO dao = new ProdutoPSemanaDAO();
    private ProdutoPSemanaClass produtoPsemana = new ProdutoPSemanaClass();
    
    private void MontarComboProduto() {
        List<ProdutoClass> lista = pdao.findall();
        
        DefaultComboBoxModel cbox = new DefaultComboBoxModel( new Vector(lista));
        
        cboxProduto.setModel(cbox);
        
        
    }
    private void MontarComboCliente() {
        List<ClientesClass> lista = clidao.findall();
        
        DefaultComboBoxModel cbox = new DefaultComboBoxModel( new Vector(lista));
        cboxCliente.setModel(cbox);
        
        
    }

    private void PreencherCampos() {
        if (modo!=1){
        txtAno.setText(""+produtoPsemana.getAno());
        txtSemana.setText(""+produtoPsemana.getNumerosemana());
        txtValor.setText(""+produtoPsemana.getValor());
        
        
        DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM  );
                     
        txtDataConclusao.setText(""+df.format(produtoPsemana.getDataconclusao()));
        //txtTempoProducao.setText(produtoPsemana.get);
        cboxCliente.getModel().setSelectedItem(produtoPsemana.getCliente());
        cboxProduto.getModel().setSelectedItem(produtoPsemana.getProduto());
        if (produtoPsemana.getCliente()!=null){
            chboxClie.setSelected(true);
        }
        if (produtoPsemana.getCliente()==null){
            chboxClie.setSelected(false);
        }
        }
        
    }
    
    
    
    
    
}


